package com.hongxun.modules.bss.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.hongxun.modules.api.entity.BEntryCriteriaEntity;
import com.hongxun.modules.api.entity.BMaterialEntity;
import com.hongxun.modules.api.service.BEntryCriteriaService;
import com.hongxun.modules.bss.entity.APWireEntity;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.hongxun.modules.bss.entity.APCriteriaEntity;
import com.hongxun.modules.bss.service.APCriteriaService;
import com.hongxun.common.utils.PageUtils;
import com.hongxun.common.utils.Query;
import com.hongxun.common.utils.R;




/**
 * 项目准入条件表
 * 
 * @author zkcn
 * @email youeremail@163.com
 * @date 2018-08-03 13:46:03
 */
@RestController
@RequestMapping("/bss/apcriteria")
public class APCriteriaController {
	@Autowired
	private APCriteriaService aPCriteriaService;
	@Autowired
	private BEntryCriteriaService bEntryCriteriaService;
	/**
	 * 列表
	 */
	@RequestMapping("/list")
	public R list(@RequestParam Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);
        String wheres =" 1=1 ";
		if(query.get("pGuid")!=null&&!query.get("pGuid").toString().equals(""))
		{
			wheres+=" and  p_guid = '" + query.get("pGuid").toString()+"' ";
		}
		if(query.get("state")!=null&&query.get("state").toString().equals("isdelno")) {
			wheres += " and  p_c_datastate != '删除' ";
		}
        query.put("filterSql",wheres+" ");
		List<APCriteriaEntity> aPCriteriaList = aPCriteriaService.queryList(query);
		int total = aPCriteriaService.queryTotal(query);
		
		PageUtils pageUtil = new PageUtils(aPCriteriaList, total, query.getLimit(), query.getPage());
		
		return R.ok().put("page", pageUtil);
	}
	
	
	/**
	 * 信息
	 */
	@RequestMapping("/info/{guid}")
	public R info(@PathVariable("guid") String guid){
		APCriteriaEntity aPCriteria = aPCriteriaService.queryObject(guid);
		
		return R.ok().put("aPCriteria", aPCriteria);
	}
	
	/**
	 * 保存
	 */
	@RequestMapping("/save")
	public R save(@RequestBody APCriteriaEntity aPCriteria){
		aPCriteriaService.save(aPCriteria);
		
		return R.ok();
	}

	@RequestMapping("/saveselect/{guid}")
	public R saveselect(@RequestBody String[] guids,@PathVariable("guid") String guid){
		for(String one :guids ) {
			Map<String, Object> params = new HashMap<String, Object>();
			String wheres = " 1=1 ";
			wheres += " and  p_guid = '" + guid + "' ";

			wheres += " and  p_c_guid = '" + one + "' ";

			params.put("filterSql", wheres + " ");
			List<APCriteriaEntity> aPWireList = aPCriteriaService.queryList(params);

			if (aPWireList.size() == 0) {

				BEntryCriteriaEntity bEntryCriteriaEntity = bEntryCriteriaService.queryObject(one);
				APCriteriaEntity aPCriteriaEntity = new APCriteriaEntity();
				aPCriteriaEntity.setPGuid(guid);
				aPCriteriaEntity.setPCGuid(one);
				aPCriteriaEntity.setPCDatastate("新增");
				aPCriteriaEntity.setPCChapter(bEntryCriteriaEntity.getECChapter());
				aPCriteriaEntity.setPCCriteria(bEntryCriteriaEntity.getECCriteria());
				aPCriteriaEntity.setPCName(bEntryCriteriaEntity.getECName());
				aPCriteriaEntity.setPCNum(bEntryCriteriaEntity.getECNum());
				aPCriteriaService.save(aPCriteriaEntity);
			}
		}
		return R.ok();
	}


	/**
	 * 修改
	 */
	@RequestMapping("/update")
	public R update(@RequestBody APCriteriaEntity aPCriteria){
		aPCriteria.setPCDatastate("修改");
		aPCriteriaService.update(aPCriteria);
		
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@RequestMapping("/delete")
	public R delete(@RequestBody String[] guids){

		aPCriteriaService.deleteBatch(guids);
		
		return R.ok();
	}
	
}
